import {Component, Input, OnDestroy, OnInit} from '@angular/core';
import {ApiService} from "@core/services/http/api.service";
import {ApesMessageService} from "@shared/apes/message";
import {DatePipe} from "@angular/common";

@Component({
  selector: 'car-minutia',
  templateUrl: './car-minutia.component.html',
  styleUrls: ['./car-minutia.component.scss']
})
export class CarMinutiaComponent implements OnInit, OnDestroy {
  @Input()
  set parmas(value:any) {
    this._parmas = value;
  }

  get parmas() {
    return this._parmas;
  }

  _parmas: any;
  rowList = {};
  jjcdhNum:any;
  cphm: any;
  mdid: any;
  rowData = [];
  pageRowIndex: number = 0;
  pageData = [];
  pageIndex: number = 0;
  private timer;
  jd: string;

  constructor(private apiService:ApiService,
              private msg: ApesMessageService,
              private datePipe: DatePipe) { }

  ngOnInit() {
    let times = (1000 * 10);
    this.timer = setInterval(() => {
      if(this.rowList['projectRemark'] !== undefined) {
        this.pageRow(this.rowList['projectRemark'], 2);
      }
      if(this.rowList['nodeItem'] !== undefined) {
        this.pageChange(this.rowList['nodeItem'], 2);
      }
      if(this.rowList['mxArray'] !== undefined) {
        this.pageChange(this.rowList['mxArray'], 3);
      }
    }, times);
  }

  getFun(key:string, cphm: string, mdid: string, jd:string) {
    this.rowList= {};
    this.rowData = [];
    this.pageData = [];
    this.jjcdhNum = key;
    this.cphm = cphm;
    this.mdid = mdid;
    this.jd = jd;
    // this.getMinutia();
    this.judge();
  }

  judge () {
    if(this.jd === '年审中') {
      if(this.parmas['lable'] === this.jd) {
        this.getMinutia();
      }
    }else {
      if(this.parmas['lable'] === undefined) {
        this.getMinutia();
      }
    }
  }

  getMinutia() {
    if (this.parmas["api"]) {
      let data = {'interfaceMethod': this.parmas["api"], 'pdata': {'JJCDH': this.jjcdhNum, 'CPHM':this.cphm, 'MDID': this.mdid}};
      this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
        (resultData) => {
          const result = JSON.parse(resultData);
          if (result.success) {
            if (result.obj.length !== 0) {
              const data = JSON.parse(result.obj);
              this.rowList = data;
              if(this.rowList['projectRemark'] !== undefined) {
                this.pageRow(this.rowList['projectRemark'], 2);
              }
              if(this.rowList['nodeItem'] !== undefined) {
                this.pageChange(this.rowList['nodeItem'], 2);
              }
              if(this.rowList['mxArray'] !== undefined) {
                this.pageChange(this.rowList['mxArray'], 3);
              }
            }
          } else {
            this.msg.info(result.msg);
          }
        });
    }
  }

  //显示时间 天/小时   小时/分钟  分钟
  toHour(minutes) {
    if (Math.floor(minutes / 60 / 24) >= 1) {
      if(Math.floor(minutes / 60 % 24) < 1) {
        return (Math.floor(minutes / 60 / 24) + '天');
      }else {
        return (Math.floor(minutes / 60 / 24) + '天' + Math.floor(minutes / 60 % 24) + '时');
      }
    } else if (Math.floor(minutes / 60 / 24) < 1) {
      if (Math.floor(minutes) < 60) {
        return (Math.floor(minutes % 60) + '分');
      }
      if(Math.floor(minutes % 60) < 1) {
        return (Math.floor(minutes / 60) + '时')
      }else {
        return (Math.floor(minutes / 60) + '时' +  Math.floor(minutes % 60) + '分');
      }
    }
  }

  // 时间戳转换 月-日 时：分
  timeChange( time: number ) {
    let day;
    if(time !== undefined ){
      day = this.datePipe.transform(new Date(time),
        'yyyy-MM-dd HH:mm:ss').slice(5, 16);
      return day;
    }
  }

  //  分页
  pageRow(arrData:any, page: number) {
    this.rowData = [];
    const count = arrData.length;
    this.rowData = [];
    let tempIndex = 0;
    for (let i = this.pageRowIndex; i < count; i++) {
      if (tempIndex < page) {
        this.rowData.push(arrData[i]);
      } else {
        break;
      }
      tempIndex++;
      this.pageRowIndex++;
    }
    //当数据遍历完 重新请求数据
    if (this.pageRowIndex >= count) {
      this.pageRowIndex = 0;
    }
  }

  //  分页
  pageChange(arrData:any, page: number) {
    this.pageData = [];
    const count = arrData.length;
    this.pageData = [];
    let tempIndex = 0;
    for (let i = this.pageIndex; i < count; i++) {
      if (tempIndex < page) {
        this.pageData.push(arrData[i]);
      } else {
        break;
      }
      tempIndex++;
      this.pageIndex++;
    }
    //当数据遍历完 重新请求数据
    if (this.pageIndex >= count) {
      this.pageIndex = 0;
    }
  }

  // 服务工单头部
  FWGDhead = [
    { lable: "项目", style: "40%" },
    { lable: "状态", style: "16%" },
    { lable: "耗时", style: "16%" },
    { lable: "预计完工", style: "23%" }
  ]

  ngOnDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  }

}
